package com.fc.v2.controller.gen;

import com.fc.v2.common.base.BaseController;
import com.fc.v2.common.domain.AjaxResult;
import com.fc.v2.common.conf.oss.OssConfig;
import com.fc.v2.common.domain.ResultTable;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.model.auto.FinishProject;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.service.FinishProjectService;
import com.fc.v2.util.ECertUtils;
import com.fc.v2.util.ImageBase64;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;

/**
 * 生成电子证书
 * @param request
 * @param response
 * @param qyId
 * @throws IOException
 */

@Controller
@RequestMapping("/CertificateController")
public class CertificateController extends BaseController {


    @Autowired
    private FinishProjectService finishProjectService;

    @Autowired
    private FileUploadController fileUploadController;

    private String prefix = "gen/certificate";


    /**
     * 结项审核申请页面展示
     * @param model
     * @return String
     * @author Archie
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("gen:finishProject:view")
    @OssConfig
    public String view(ModelMap model)
    {
        return prefix + "/list";
    }

    /**
     * 查看证书页面展示
     * @param model
     * @return String
     * @author Archie
     */
    @ApiOperation(value = "查看证书", notes = "查看证书")
    @GetMapping("/check/{id}")
    @OssConfig
    public String check(@PathVariable("id") String id, ModelMap modelMap)
    {
        StringBuilder fileNameBuilder = new StringBuilder();
        FinishProject finishProject = finishProjectService.selectByPrimaryKey(id);
        String proName = finishProject.getProName();
        Date date = finishProject.getCertUpdateTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String fileName = fileNameBuilder.append(proName).append(year).append(month).append(day).toString();
        String path = "C:_Publish//社科联//生成证书//"+fileName+".png";
        String imageBase64 = ImageBase64.ImageToBase64(path);
        modelMap.put("imageBase64",imageBase64);
        modelMap.put("fileName",fileName);
        modelMap.put("path",path);
        return prefix + "/check";
    }

    /**
     * list集合
     * @param tablepar
     * @param searchText
     * @return
     */
    //@Log(title = "结项审核申请", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list")
    @RequiresPermissions("gen:finishProject:list")
    @ResponseBody
    public ResultTable list(Tablepar tablepar, FinishProject finishProject){
        PageInfo<FinishProject> page=finishProjectService.listCert(tablepar,finishProject) ;
        return pageTable(page.getList(),page.getTotal());
    }

    /**
     * 下载证书
     * @param ids
     * @return
     */
    //@Log(title = "下载证书", action = "111")
    @ApiOperation(value = "下载证书", notes = "下载证书")
    @PostMapping("/download/{id}")
    @ResponseBody
    public Map<String, Object> remove(@PathVariable("id") String id){
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder fileNameBuilder = new StringBuilder();
        FinishProject finishProject = finishProjectService.selectByPrimaryKey(id);
        String proName = finishProject.getProName();
        Date date = finishProject.getCertUpdateTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String fileName = fileNameBuilder.append(proName).append(year).append(month).append(day).toString();
        String path = "C:_Publish////社科联////生成证书////"+fileName+".png";
        map.put("fileName",fileName);
        map.put("path",path);
        return map;
    }



    @PostMapping("/testECert")
    public void testECert(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //JSONObject jsonObject =new JSONObject();
        //System.err.println("qyId:"+qyId);
//        zybEnterpriseVo zybEnterpriseVo2 = new zybEnterpriseVo();
//        zybEnterpriseVo2.setId(qyId);
//        zybEnterpriseVo zybEnterpriseVo = zybEnterpriseVoService.selectQyItemById(zybEnterpriseVo2);
//        Map<String,Object> resultMap = ECertUtils.generateECert(request,response);
//        System.err.println("resultMap: "+resultMap);
//       if(resultMap.get("result").equals("0")){
//           jsonObject.put("message","生成失败!");
//       }else{
//           jsonObject.put("message","生成成功!");
//       }
//        jsonObject.put("code",resultMap.get("result"));
    }


}
